﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Ylh.BLL;
using ExtAspNet;


public partial class Module_CustManger_CustFlow : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.OnClientClick = ActiveWindow.GetHideReference();
        if (!IsPostBack)
        {
            OtherFollow.Hidden = true;
            LoadData(Request.QueryString["CustID"].ToString());
        }


        if (Request.QueryString ["enable"]!=null)
        {
            FollowType.Readonly = OtherFollow.Readonly = FollowTime.Readonly = FollowSituation.Readonly =Intentions.Readonly=Remark.Readonly=NextFlow.Readonly= true;
            Button1.Hidden = true;
        }



    }

    private void LoadData(string CustID)
    {
        CustBLL bllc = new CustBLL(DbConnectionStr);
        Label2.Text = bllc.GetCustName(CustID);

        if (Request.QueryString["CustFollow"] != null)
        {
            CustFollow.Text = Request.QueryString["CustFollow"].ToString();
            loadCustFollow(CustFollow.Text);
        }



    }

    private void loadCustFollow(string CustFollow)
    {
        CustFollowBLL bllf = new CustFollowBLL(DbConnectionStr);
        Ylh.Model.TbCustFollow flow = bllf.GetModel(Convert.ToInt32(CustFollow));

        FollowType.SelectedValue = flow.FollowType;

        if (flow.FollowType=="其他")
        {
            OtherFollow.Hidden = false;
        }

        OtherFollow.Text = flow.OtherFollow;

        FollowTime.SelectedDate = flow.FollowTime;

        FollowSituation.Text = flow.FollowSituation;

        Intentions.SelectedValue = flow.Intentions;
        Remark.Text = flow.Remark;

        NextFlow.SelectedDate  = flow.NextFlow;


    }





    protected void FollowType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (FollowType.SelectedValue == "其他")
        {
            OtherFollow.Hidden = false;
        }
        else
        {
            OtherFollow.Hidden = true;
            OtherFollow.Text = string.Empty;
        }
    }


    private Ylh.Model.TbCustFollow Getmodel()
    {
        int CustID = Convert.ToInt32(Request.QueryString["CustID"]);
        int StaffID = Convert.ToInt32(base.StaffID);
        string FollowType = this.FollowType.SelectedValue;
        string OtherFollow = this.OtherFollow.Text;
        DateTime FollowTime = DateTime.Parse(this.FollowTime.Text);
        string FollowSituation = this.FollowSituation.Text;

        Ylh.Model.TbCustFollow model = new Ylh.Model.TbCustFollow();

        if (!string.IsNullOrEmpty(CustFollow.Text))
        {
            model.CustFollow = Convert.ToInt32(CustFollow.Text);
        }


        model.CustID = CustID;
        model.StaffID = StaffID;
        model.FollowType = FollowType;
        model.OtherFollow = OtherFollow;
        model.FollowTime = FollowTime;
        model.FollowSituation = FollowSituation;

        model.Intentions = Intentions.SelectedValue;

        model.Remark = Remark.Text;

        if (!string.IsNullOrEmpty(NextFlow.Text))
        {
            model.NextFlow = Convert.ToDateTime(NextFlow.Text);
        }
        else
        {
            model.NextFlow = null;
        }


        return model;
    }






    protected void Button1_Click(object sender, EventArgs e)
    {
        Ylh.Model.TbCustFollow model = Getmodel();
        CustFollowBLL bllf = new CustFollowBLL(DbConnectionStr);
        if (string.IsNullOrEmpty(CustFollow.Text))
        {

            CustFollow.Text = bllf.InsertFlowInfo(model).ToString();
            //Alert.Show("跟进信息添加成功");
        }
        else
        {
            bllf.UpdateFlowInFo(model);
            //Alert.Show("跟进信息修改成功");
        }

        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());


    }
}